/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AlterCustomer.java
 *
 * Created on 21-nov-2011, 11:12:54
 */
package view;

import javax.swing.ImageIcon;
import main.ValidateInput;
import main.WinkelApplication;
import model.Customer;

/**
 *
 * 
 */
public class AlterCustomer extends javax.swing.JPanel {
    boolean modify;
    boolean selected;
    int customerID;
    
    /** Creates new form AlterCustomer */
    public AlterCustomer(boolean modify, boolean selected, int customerID) {
        initComponents();
        ImageIcon test = new ImageIcon("test.gif");
        //btnBack.
        
        this.modify = modify;
        this.selected = selected;
        this.customerID = customerID;
        
        if(this.modify){
        Customer customer = WinkelApplication.getQueryManager().getCustomer(customerID);
        txtCustomerID.setText(String.valueOf(this.customerID));
        txtName.setText(customer.getName());
        txtCity.setText(customer.getCity());
        txtAddress.setText(customer.getAddress());
        txtPostalcode.setText(customer.getZipcode());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblName = new javax.swing.JLabel();
        lblAddress = new javax.swing.JLabel();
        lblPostalcode = new javax.swing.JLabel();
        lblCity = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtAddress = new javax.swing.JTextField();
        txtPostalcode = new javax.swing.JTextField();
        txtCity = new javax.swing.JTextField();
        btnSave = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();
        lblCustomerID = new javax.swing.JLabel();
        txtCustomerID = new javax.swing.JTextField();

        lblName.setText("Naam:");

        lblAddress.setText("Adres:");

        lblPostalcode.setText("Postcode:");

        lblCity.setText("Woonplaats:");

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgSave.gif"))); // NOI18N
        btnSave.setText("Opslaan");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgBack.png"))); // NOI18N
        btnBack.setText("Terug");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        lblCustomerID.setText("Klanten_ID");

        txtCustomerID.setEditable(false);
        txtCustomerID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCustomerIDActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lblName)
                        .addGap(40, 40, 40)
                        .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblAddress)
                        .addGap(39, 39, 39)
                        .addComponent(txtAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblPostalcode)
                        .addGap(23, 23, 23)
                        .addComponent(txtPostalcode, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblCity)
                        .addGap(10, 10, 10)
                        .addComponent(txtCity, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnBack)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 208, Short.MAX_VALUE)
                        .addComponent(btnSave))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblCustomerID)
                        .addGap(18, 18, 18)
                        .addComponent(txtCustomerID, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCustomerID)
                    .addComponent(txtCustomerID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAddress)
                    .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPostalcode)
                    .addComponent(txtPostalcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCity)
                    .addComponent(txtCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBack)
                    .addComponent(btnSave))
                .addContainerGap(37, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        WinkelApplication.getInstance().showPanel(new CustomerOverview(this.selected));
    }//GEN-LAST:event_btnBackActionPerformed
 
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
                Customer customer = new Customer();
                customer.setNaam(txtName.getText());
                customer.setAdres(txtAddress.getText());
                customer.setPostcode(txtPostalcode.getText());
                customer.setWoonplaats(txtCity.getText());
                if(ValidateInput.ValidateMissingCustomer(customer).equalsIgnoreCase("")){
                    if(modify){
                        customer.setKlantId(Integer.parseInt(txtCustomerID.getText()));
                        WinkelApplication.getQueryManager().changeCustomer(customer);
                    }else {
                        WinkelApplication.getQueryManager().addCustomer(customer);
                    }
                WinkelApplication.getInstance().showPanel(new CustomerOverview(this.selected));
                } else {
                    WinkelApplication.msgbox(ValidateInput.ValidateMissingCustomer(customer));
                }
    }//GEN-LAST:event_btnSaveActionPerformed

private void txtCustomerIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCustomerIDActionPerformed

}//GEN-LAST:event_txtCustomerIDActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel lblAddress;
    private javax.swing.JLabel lblCity;
    private javax.swing.JLabel lblCustomerID;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblPostalcode;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtCity;
    private javax.swing.JTextField txtCustomerID;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPostalcode;
    // End of variables declaration//GEN-END:variables
}
